from google.appengine.ext import webapp, gql
from google.appengine.ext.webapp.util import run_wsgi_app
from models import Business, Deal, Comment
from test.test_typechecks import Integer
import util
import string
from util import get_deal_from_xml, get_comment_from_xml
import logging

##
#this file main role is to handle with rate deals

class DataBaseRateDealHandler(webapp.RequestHandler):

    def get(self): 
        self.response.out.write(''' 
        <html> 
          <body> 
            <form method="post"> 
              <p>XML: <input type="string" name="xml" /></p>
              <p>Rate: <input type="integer" name="rate" /></p>
              <p>Cost_Level: <input type="integer" name="cost_level" /></p>
              <p><input type="submit" /></p>  
            </form> 
          </body> 
        </html> 
        ''') 
        
    def post(self):
        logging.info("Start rate deal request")
        
        self.response.headers['Content-Type'] = 'text/xml'
        
        #get the xml
        xml = self.request.get("xml", "")
        rate =  self.request.get("rate", "")
        cost_level = self.request.get("cost_level", "")
        
        logging.debug("rate = " + unicode(xml) + ", cost_level = " + unicode(cost_level))
        logging.debug("xml got:")
        logging.debug(unicode(xml))
        
        deal = get_deal_from_xml(xml, produce_new=False, update_changes=False)
        if (deal==None):
            logging.info("Finish rate deal request: failed")
            self.response.out.write(util.produce_result_xml("failed"))
        #rate
        if (rate != ""):
            logging.info("rating")
            deal.rate_num = deal.rate_num + 1
            deal.rate_sum = deal.rate_sum +  string.atoi(rate)
        #cost level
        if (cost_level != ""):
            logging.info("cost leveling")
            deal.cost_level_num = deal.cost_level_num + 1
            deal.cost_level_sum = deal.cost_level_sum +  string.atoi(cost_level)
        
        deal.save()
        
        logging.info("Finish rate deal request: success")
        self.response.out.write(util.produce_result_xml("success"))
        
class DataBaseRateDealFakeHandler(webapp.RequestHandler):

    def get(self): 
        self.response.out.write(''' 
        <html> 
          <body> 
            <form method="post"> 
              <p>XML: <input type="string" name="xml" /></p>
              <p>User_name: <input type="string" name="user_name" /></p>
              <p>Email: <input type="string" name="email" /></p>
              <p><input type="submit" /></p>  
            </form> 
          </body> 
        </html> 
        ''') 
        
    def post(self):
        xml = self.request.get("xml", "")
        user_name = self.request.get("user_name", "")
        email = self.request.get("email", "")
        
        result = util.validate_user_credit(user_name, email)
        if (not result):
            self.response.out.write(util.produce_result_xml("failed"))
            return
        
        deal = get_deal_from_xml(xml, produce_new=False, update_changes=False)
        if (deal != None):
            logging.info("fake deal reported")
            #add to fake_num
            deal.fake_num = deal.fake_num + 1
            #add rate 0
            deal.rate_num = deal.rate_num + 1
            deal.save()
            #add comment fake
            comment = Comment()
            comment.deal_item = deal
            comment.comment_str = "This deal reported as fake."
            comment._source = email
            comment._source_type = "user"
            comment.user_name = user_name
            comment.put()
            
            self.response.headers['Content-Type'] = 'text/xml'
            self.response.out.write(util.produce_result_xml("success"))
            logging.info("Finish rate deal fake request: success")
        else:
            self.response.headers['Content-Type'] = 'text/xml'
            self.response.out.write(util.produce_result_xml("failed"))
            logging.info("Finish rate deal fake request: failed")
            
            
        
        
class DataBaseRateCommentHandler(webapp.RequestHandler):

    def get(self): 
        self.response.out.write(''' 
        <html> 
          <body> 
            <form method="post"> 
              <p>XML: <input type="string" name="xml" /></p>
              <p>Rate: <input type="integer" name="rate" /></p>
              <p><input type="submit" /></p>  
            </form> 
          </body> 
        </html> 
        ''') 
        
    def post(self):
        logging.info("Starting rate comment request")
        
        self.response.headers['Content-Type'] = 'text/xml'
        #get the xml
        xml = self.request.get("xml", "")
        rate =  self.request.get("rate", "")
        
        logging.debug("rate = " + unicode(xml))
        logging.debug("xml got:")
        logging.debug(unicode(xml))
        
        comment = get_comment_from_xml(xml)
        
        if (comment==None):
            self.response.out.write(util.produce_result_xml("failed"))
            logging.info("Finish rate comment request: failed")
        comment.comment_rate_num =comment.comment_rate_num + 1
        comment.comment_rate_sum = comment.comment_rate_sum +  string.atoi(rate)
        comment.save()
        
        self.response.out.write(util.produce_result_xml("success"))
        
        logging.info("Finish rate comment request: success")

application = webapp.WSGIApplication([('/database/rate/deal', DataBaseRateDealHandler),
                                      ('/database/rate/deal/fake', DataBaseRateDealFakeHandler),
                                           ('/database/rate/comment', DataBaseRateCommentHandler)], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
